package com.utils.bean;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


import org.apache.commons.beanutils.PropertyUtils;

/**
 * Bean 转换工具
 */
public class BeanUtil {

	

	/**
	 * 将map中的column key 转换为 field key
	 */
	public static Map<String, Object> columnToFieldKey(Map<String, Object> map) {
		Map<String, Object> newMap = new HashMap<String, Object>();
		for (Map.Entry<String, Object> en : map.entrySet()) {
			newMap.put(columnSetMethod(en.getKey()), en.getValue());
		}
		return newMap;
	}



	

	// 获取属性的值
	public static Object getProperty(Object bean, String fieldName) throws Exception {
		Class<?> fieldType = PropertyUtils.getPropertyType(bean, fieldName.trim());
		if (fieldType != null) {
			return PropertyUtils.getProperty(bean, fieldName.trim());
		}
		return null;
	}

	

	/**
	 * 获取属性类型
	 * 
	 * @param bean
	 * @param property
	 * @return
	 * @throws Exception
	 */
	public static Class<?> getPropertyType(Object bean, String property) throws Exception {
		try {
			Field field = bean.getClass().getDeclaredField(property);
			if (field != null)
				return PropertyUtils.getPropertyType(bean, property);
		}
		catch (SecurityException e) {
			e.printStackTrace();
		}
		catch (NoSuchFieldException e) {
			return null;
		}
		return null;
	}

	/**
	 * 数据库字段转换为set方法
	 * 
	 * @param column
	 * @return
	 */
	public static String columnSetMethod(String column) {
		return "set" + columnToField2(column);
	}

	/**
	 * 转换属性到数据库字段
	 * 
	 * @param clazz
	 * @return
	 */
	public static Map<String, String> getAllFieldColumns(Class<?> clazz) {
		if (clazz == null) {
			return null;
		}
		try {
			Map<String, String> map = new LinkedHashMap<String, String>();
			Field[] fields = clazz.getDeclaredFields();
			if (fields != null) {
				for (Field field : fields) {
					if (!"serialVersionUID".equals(field.getName())) {// 序列化ID不需要
						String fieldName = field.getName();
						String colName = BeanUtil.fieldToColumn(fieldName);
						map.put(fieldName, colName);
					}
				}
			}
			Class<?> superClass = clazz.getSuperclass();// 递归获取父类的Field
			Map<String, String> superMap = getAllFieldColumns(superClass);
			if (superMap != null) {
				map.putAll(superMap);
			}
			if (map.size() == 0) {
				return null;
			}
			return map;
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 转换有下划线的String，下划线的后一个字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String columnToField(String str) {
		String[] arr = str.split("_");
		if (arr != null && arr.length > 1) {
			String rstStr = arr[0];
			for (int i = 1; i < arr.length; i++) {
				rstStr += arr[i].substring(0, 1).toUpperCase() + arr[i].substring(1);
			}
			return rstStr;
		}
		else {
			return str;
		}
	}

	/**
	 * 转换有下划线的String，第一个字母大写，下划线的后一个字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String columnToField2(String str) {
		String[] arr = str.split("_");
		if (arr != null && arr.length > 1) {
			String rstStr = arr[0].substring(0, 1).toUpperCase() + arr[0].substring(1);
			for (int i = 1; i < arr.length; i++) {
				rstStr += arr[i].substring(0, 1).toUpperCase() + arr[i].substring(1);
			}
			return rstStr;
		}
		else {
			return str.substring(0, 1).toUpperCase() + str.substring(1);
		}
	}

	/**
	 * 转换将第一个字母大写变成小写，并在前面加下划线
	 * 
	 * @param args
	 */
	public static String fieldToColumn(String str) {
		char[] chars = str.toCharArray();
		String rstStr = "";
		for (int i = 0; i < chars.length; i++) {
			if (chars[i] > 64 && chars[i] < 94) {
				rstStr += ("_" + chars[i]).toLowerCase();
			}
			else {
				rstStr += chars[i];
			}
		}
		return rstStr;
	}

	/**
	 * 首字母变大写
	 * 
	 * @param args
	 */
	public static String upperCaseFirst(String str) {
		return Character.toUpperCase(str.charAt(0)) + str.substring(1, str.length());
	}

	/**
	 * 首字母变小写
	 * 
	 * @param args
	 */
	public static String lowerCaseFirst(String str) {
		return Character.toLowerCase(str.charAt(0)) + str.substring(1, str.length());
	}

}
